package com.fwtai.service.api;

import com.fwtai.api.ApiUserBankDao;
import com.fwtai.bean.PageFormData;
import com.fwtai.config.LocalUserId;
import com.fwtai.tool.ToolClient;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.CompletableFuture;

/**
 * 用户提现信息业务层
 * @作者 田应平
 * @版本 v1.0
 * @QQ号码 444141300
 * @创建日期 2023-04-14 17:20:03
 * @官网 <url>http://www.fwtai.com</url>
*/
@Service
public class ApiUserBankService{

  private final ApiUserBankDao apiUserBankDao;

  private final ThreadPoolTaskExecutor taskExecutor;

  public ApiUserBankService(final ApiUserBankDao apiUserBankDao,final ThreadPoolTaskExecutor taskExecutor){
    this.apiUserBankDao = apiUserBankDao;
    this.taskExecutor = taskExecutor;
  }

  public String add(final HttpServletRequest request){
    final String loginId = LocalUserId.get();
    return CompletableFuture.supplyAsync(()->{
      if(loginId == null) return ToolClient.tokenInvalid();
      final PageFormData formData = new PageFormData(request);
      final String p_real_name = "real_name";
      final String p_id_card = "id_card";
      final String p_bank_name = "bank_name";
      final String p_bank_code = "bank_code";
      final String validate = ToolClient.validateField(formData,p_real_name,p_id_card,p_bank_name,p_bank_code);
      if(validate != null) return validate;
      final String len_real_name = formData.getString(p_real_name);
      if(len_real_name != null && len_real_name.length() > 64){
        return ToolClient.createJsonFail("真实姓名字数太多");
      }
      final String len_id_card = formData.getString(p_id_card);
      if(len_id_card != null && len_id_card.length() > 20){
        return ToolClient.createJsonFail("身份证号码字数太多");
      }
      final String len_bank_name = formData.getString(p_bank_name);
      if(len_bank_name != null && len_bank_name.length() > 64){
        return ToolClient.createJsonFail("开户银行名称（开户行）字数太多");
      }
      final String len_bank_code = formData.getString(p_bank_code);
      if(len_bank_code != null && len_bank_code.length() > 40){
        return ToolClient.createJsonFail("银行卡号字数太多");
      }
      formData.put("user_id",Long.parseLong(loginId));
      return ToolClient.executeRows(apiUserBankDao.add(formData));
    },taskExecutor).join();
  }

  public String edit(final HttpServletRequest request){
    final String loginId = LocalUserId.get();
    return CompletableFuture.supplyAsync(()->{
      if(loginId == null) return ToolClient.tokenInvalid();
      final PageFormData formData = new PageFormData(request);
      final String p_real_name = "real_name";
      final String p_id_card = "id_card";
      final String p_bank_name = "bank_name";
      final String p_bank_code = "bank_code";
      final String p_user_id = "user_id";
      final String validate = ToolClient.validateField(formData,p_real_name,p_id_card,p_bank_name,p_bank_code);
      if(validate != null) return validate;
      final String len_real_name = formData.getString(p_real_name);
      if(len_real_name != null && len_real_name.length() > 64){
        return ToolClient.createJsonFail("真实姓名字数太多");
      }
      final String len_id_card = formData.getString(p_id_card);
      if(len_id_card != null && len_id_card.length() > 20){
        return ToolClient.createJsonFail("身份证号码字数太多");
      }
      final String len_bank_name = formData.getString(p_bank_name);
      if(len_bank_name != null && len_bank_name.length() > 64){
        return ToolClient.createJsonFail("开户银行名称（开户行）字数太多");
      }
      final String len_bank_code = formData.getString(p_bank_code);
      if(len_bank_code != null && len_bank_code.length() > 40){
        return ToolClient.createJsonFail("银行卡号字数太多");
      }
      final long loginKey = Long.parseLong(loginId);
      final Long v = apiUserBankDao.queryExistById(loginKey);
      if(v == null){
        return ToolClient.createJsonFail("数据不存在,刷新重试");
      }
      formData.put(p_user_id,loginKey);
      return ToolClient.executeRows(apiUserBankDao.edit(formData));
    },taskExecutor).join();
  }

  public String queryById(){
    final String user_id = LocalUserId.get();
    return CompletableFuture.supplyAsync(()->{
      if(user_id == null){
        return ToolClient.jsonValidateField();
      }
      return ToolClient.queryJson(apiUserBankDao.queryById(Long.parseLong(user_id)));
    },taskExecutor).join();
  }
}